import axios from 'axios'
import { getCookie } from '@/utils/httpParamsHandler';
const service = axios.create({
  baseURL:import.meta.env.VITE_BASE_API, //'https://farming.tst.cqtancheng.com/',
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})

// request拦截器
service.interceptors.request.use(config => {
  config.headers['Authorization'] = getCookie('userToken') ||'NmE1MDc0MGItZDU5OC00MjZiLTljYWUtZjdjNjY1MzRkMTJk'
  config.headers['Token'] = getCookie('userToken') || 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6IjEzNjI4NDY2MTY2IiwiZXhwIjoxNzIyNDc5NTI4LCJ1c2VySWQiOiI5OCJ9.kEOW1s6cO3AKuVHL6ycuOh1LgIlwy_qWwjL_lvWxHaU'
  config.headers['X-Request-Id'] = 1
  return config
}, error => {
  Promise.reject(error)
})

// 响应拦截
service.interceptors.response.use(res => {
  if(!res.data.success && res.data.message=="需要登录"){
  }
  return res
}, error => {
  if(error.response.status == 500){
    return Promise.resolve(error.response)
  }else{
    return Promise.resolve(error.response)
  }
})

export default service
